package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

//    /**
//     * 向购物车里添加菜品或套餐
//     * 传过来数据没有表中number,需要自己手动放入
//     * 添加购物车有重复添加的过程,添加之前需要执行判断所添加菜品或套餐是否已经存在
//     * @param shoppingCart
//     * @return
//     */
//    @PostMapping("/add")
//    public R<String> save(@RequestBody ShoppingCart shoppingCart){
//        //打印接收信息
//        log.info("购物车数据:{}",shoppingCart);
//        //判断购物车里是否有该用户加入的该商品(number==0 where userid+dish_or_setmeal)
//        //userid
//        Long id = BaseContext.getCurrentId();
//        //存入id
//
//        //
//        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,id);
//        //判断传来的数据是菜品还是套餐,添加查询条件
//        Long dishId = shoppingCart.getDishId();
//        if(dishId==null){
//            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
//        }else{
//            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
//        }
//        ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
//        //1.如果没有,执行save,设置number属性
//        if(one==null){
//            shoppingCart.setNumber(1);
//            shoppingCart.setCreateTime(LocalDateTime.now());
//            shoppingCart.setUserId(id);
//            shoppingCartService.save(shoppingCart);
//        }
//        //2.如果有,执行update,修改number属性
//        //判断添加的是菜品还是套餐
//        else {
//            one.setNumber(one.getNumber()+1);
//            shoppingCartService.updateById(one);
//        }
//        return R.success("添加购物车成功");
//    }
//
//    /**
//     * 查询所有购物车商品
//     * @return
//     */
//    @GetMapping("/list")
//    public R<List<ShoppingCart>> list(){
//        log.info("查看购物车");
//        Long currentId = BaseContext.getCurrentId();
//        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
//        shoppingCartLambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);
//        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
//        return R.success(list);
//    }
//
//    @DeleteMapping("/clean")
//    public R<String> clean(){
//        log.info("清空购物车");
//        Long currentId = BaseContext.getCurrentId();
//        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
//        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
//        return R.success("清空购物车成功");
//    }
}
